package com.gx.smart.building.ui.swap.fragment

import android.content.Intent
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import com.gx.smart.base.BaseAdapter
import com.gx.smart.base.BaseMVVMFragmentV2
import com.gx.smart.building.R
import com.gx.smart.building.databinding.FragmentSwapDetailBinding
import com.gx.smart.building.ui.common.adapter.ImageAdapter
import com.gx.smart.building.ui.repair.RepairDetailActivity
import com.gx.smart.building.ui.swap.ModifySwapActivity
import com.gx.smart.building.ui.swap.WaitingSignActivity
import com.gx.smart.building.ui.swap.adapter.SwapDeviceConfirmResultAdapter
import com.gx.smart.building.ui.swap.adapter.SwapDeviceResultAdapter
import com.gx.smart.building.ui.swap.adapter.SwapDeviceStatusResultAdapter
import com.gx.smart.building.ui.swap.adapter.SwapRepairAdapter
import com.gx.smart.building.ui.swap.adapter.SwapWarningAdapter
import com.gx.smart.building.ui.swap.model.SwapContentMessageEvent
import com.gx.smart.building.ui.swap.viewmodel.SwapDetailViewModel
import com.gx.smart.building.ui.warning.WarningDetailActivity
import com.gx.smart.lib_http.BuildConfig
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.ClickDto
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.RecordDto
import org.greenrobot.eventbus.EventBus

class SwapDetailFragment : BaseMVVMFragmentV2<SwapDetailViewModel>() {
    private lateinit var viewBinding: FragmentSwapDetailBinding
    private lateinit var stateAdapter: SwapDeviceResultAdapter
    private lateinit var recordAdapter: SwapDeviceStatusResultAdapter
    private lateinit var eventAdapter: SwapDeviceConfirmResultAdapter
    private lateinit var workOrderAdapter: SwapRepairAdapter
    private lateinit var alarmAdapter: SwapWarningAdapter
    private lateinit var imageAdapter: ImageAdapter
    private lateinit var item: RecordDto
    override fun onBindLayout(): Int = R.layout.fragment_swap_detail

    override fun getToolbarTitle(): String = "交接班"

    override fun onBindViewModel(): Class<SwapDetailViewModel> = SwapDetailViewModel::class.java

    override fun initContentView(root: ViewGroup) {
        viewBinding = FragmentSwapDetailBinding.inflate(layoutInflater, root, true)
        stateAdapter = SwapDeviceResultAdapter(requireContext())
        viewBinding.deviceRecyclerView.adapter = stateAdapter
        recordAdapter = SwapDeviceStatusResultAdapter(requireContext())
        viewBinding.deviceStatusRecyclerView.adapter = recordAdapter
        eventAdapter = SwapDeviceConfirmResultAdapter(requireContext())
        viewBinding.deviceConfirmRecyclerView.adapter = eventAdapter
        workOrderAdapter = SwapRepairAdapter(requireContext())
        workOrderAdapter.setItemClickListener(object : BaseAdapter.OnItemClickListener<ClickDto> {
            override fun onItemClick(e: ClickDto, position: Int) {
                val intent = Intent(requireActivity(), RepairDetailActivity::class.java)
                intent.putExtra("id", e.clickId)
                startActivity(intent)
            }
        })

        viewBinding.workOrderRecyclerView.adapter = workOrderAdapter
        alarmAdapter = SwapWarningAdapter(requireContext())
        alarmAdapter.setItemClickListener(object : BaseAdapter.OnItemClickListener<ClickDto> {
            override fun onItemClick(e: ClickDto, position: Int) {
                val intent = Intent(requireActivity(), WarningDetailActivity::class.java)
                intent.putExtra("id", e.clickId)
                startActivity(intent)
            }
        })

        viewBinding.alarmRecyclerView.adapter = alarmAdapter
        imageAdapter = ImageAdapter(requireContext())
        viewBinding.imageRecyclerView.adapter = imageAdapter

        viewBinding.modify.setOnClickListener {
            modify()
        }
        viewBinding.qrcode.setOnClickListener {
            qrcode()
        }
    }

    override fun initData() {
        val id = requireActivity().intent.getStringExtra("id")!!
        viewModel.queryAppScheduleRecordDetail(id)
            .observe(viewLifecycleOwner) {
                item = it
                bindUI(it)
            }
    }

    private fun bindUI(item: RecordDto) {
        //App专用 1我的交班 2我的接班
        if (item.recordType == 1) {
            viewBinding.title.text = "我的交班"
            viewBinding.type.text = "接班人"
            viewBinding.shiftPerson.text = item.overPerson
        } else {
            viewBinding.title.text = "我的接班"
            viewBinding.type.text = "交班人"
            //交班人
            viewBinding.shiftPerson.text = item.shiftPerson
        }
        //交接单状态 1未签收 2已签收 预留（3提前签收 4超时签收）
        if (item.status == 2) {
            viewBinding.submitLayout.visibility = View.GONE
            viewBinding.signatureLayout.visibility = View.VISIBLE
            //签名
            Glide.with(this).load(BuildConfig.FILE_UPLOAD_URL + item.nameFile)
                .into(viewBinding.signatureImage)
            viewBinding.status.text = "已完成"
            viewBinding.status.setTextColor(resources.getColor(R.color.font_color_style_24))
            viewBinding.status.setBackgroundResource(R.drawable.shape_status_green_text_view_background)
        } else {
            viewBinding.submitLayout.visibility = View.VISIBLE
            viewBinding.signatureLayout.visibility = View.GONE
            viewBinding.status.text = "未签收"
            viewBinding.status.setTextColor(resources.getColor(R.color.font_color_style_eleven))
            viewBinding.status.setBackgroundResource(R.drawable.shape_status_text_view_background)
        }

        viewBinding.organName.text = item.organName
        viewBinding.scheduleName.text = item.scheduleName
        //2022-08-19 11:30-12:00
        val begin = TimeUtils.millis2String(item.floatTimeBegin, "yyyy-MM-dd HH:mm")
        val end = TimeUtils.millis2String(item.floatTimeEnd, "HH:mm")
        viewBinding.floatTimeBeginEnd.text = "$begin-$end"
        //交接内容
        stateAdapter.refresh(item.stateList)
        recordAdapter.refresh(item.recordList)
        eventAdapter.refresh(item.eventList)
        //提醒事项
        viewBinding.remind.text = item.remind ?: ""
        //关联事项
        if (item.workOrderContentCount == 0) {
            viewBinding.workOrderLabel.text = "维修工单：无"
        } else {
            workOrderAdapter.refresh(item.workOrderContentList)
        }
        if (item.alarmContentCount == 0) {
            viewBinding.alarmLabel.text = "报警记录：无"
        } else {
            alarmAdapter.refresh(item.alarmContentList)
        }

        //图片上传
        imageAdapter.refresh(item.fileList.map { BuildConfig.FILE_UPLOAD_URL + it.fileUrl })
    }


    private fun qrcode() {
        val id = requireActivity().intent.getStringExtra("id")!!
        val intent = Intent(requireActivity(), WaitingSignActivity::class.java)
        intent.putExtra("id", id)
        startActivity(intent)
        requireActivity().finish()
    }

    private fun modify() {
        startActivity(Intent(requireActivity(), ModifySwapActivity::class.java))
        EventBus.getDefault().postSticky(SwapContentMessageEvent(item))
        requireActivity().finish()
    }


}